//
//  NSString+Extension.m
//  CatalystOA
//
//  Created by ADMIN on 2017/2/14.
//
//

#import "NSString+Extension.h"
#import "EncryptionTools.h"

#define G_DES_IV @""
#define G_DES_Key @""

@implementation NSString (Extension)
+(NSString *)stringWithDESEncrypt:(NSString *)aString {
    NSString *des_iv = G_DES_IV;
    NSData *des_Data = [des_iv dataUsingEncoding: NSUTF8StringEncoding];
    EncryptionTools *encryptionTools = [EncryptionTools sharedEncryptionTools];
    encryptionTools.algorithm = kCCAlgorithmDES;
    NSString *pwd = [encryptionTools encryptString:aString keyString:G_DES_Key iv:des_Data];
    return pwd;
}

+ (NSString *)stringWithDecryptString:(NSString *)aString {
    NSData *testData = [G_DES_IV dataUsingEncoding: NSUTF8StringEncoding];
    EncryptionTools *shar = [EncryptionTools sharedEncryptionTools]; // f3JIQbaFLik=
    shar.algorithm = kCCAlgorithmDES;
    return [shar decryptString:aString keyString:G_DES_Key iv:testData];
}

//Base64加密
+(NSString *)encodeBase64:(NSString *)input{
    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    data = [GTMBase64 encodeData:data];
    NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"encodeBase64 == %@",base64String);
    return base64String;
}

//Base64编码
+(NSString *)base64EncodeString:(NSString *)string
{
    //1.先把字符串转换为二进制数据
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    //2.对二进制数据进行base64编码，返回编码后的字符串
    //这是苹果已经给我们提供的方法
    NSString *str = [data base64EncodedStringWithOptions:0];
    return str;
}

//Base64解密
+(NSString *)decodeBase64:(NSString *)input{
    NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    data = [GTMBase64 decodeData:data];
    NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"decodeBase64 == %@",base64String);
    return base64String;
}

//对base64编码后的字符串进行解码
+(NSString *)base64DecodeString:(NSString *)string
{
    //1.将base64编码后的字符串『解码』为二进制数据
    //这是苹果已经给我们提供的方法
    NSData *data = [[NSData alloc]initWithBase64EncodedString:string options:0];
    //2.把二进制数据转换为字符串返回
    NSString *str =  [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    return str;
}

//Base64 字符串解码成 字符串
+(NSString *)decodeBase64ToHexString:(NSString *)input{
    //1.将base64编码后的字符串『解码』为二进制数据
    //这是苹果已经给我们提供的方法
    NSData *myD = [[NSData alloc]initWithBase64EncodedString:input options:0];
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        if([newHexStr length]==1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    return hexStr;
    
}




@end
